const { ListNode, LinkedList } = require('../2. 链表/1. 链表基础/1. 建立线性链表.js')
// 给定一个已排序的链表的头head
// 要求删除原始链表中所有重复数字的节点，只留下不同的数字，返回已排序的链表
// 思路，这个题要把所有的重复元素都去掉，需要借助虚拟节点实现判断

function deleteDuplicates(head) {
    let dummyHead = new ListNode(0, head)
    let cur = dummyHead
    while (cur.next && cur.next.next) {
        if (cur.next.val === cur.next.next.val) {
            let temp = cur.next
            while (temp && temp.next && temp.val === temp.next.val) {
                temp = temp.next
            }
            cur.next = temp.next
        } else {
            cur = cur.next
        }
    }
    return dummyHead.next
}

let head = [1, 1, 1, 2, 3]
let node = new LinkedList(head)
console.log(deleteDuplicates(node.head));